package com.tansuo365.test1.service.excelservice.excelimpl;

import com.tansuo365.test1.bean.InfoData;
import com.tansuo365.test1.bean.otherbean.DWuliao;
import com.tansuo365.test1.bean.otherbean.ShengouDetails;
import com.tansuo365.test1.listener.UploadSengouDetailsDataListener;
import com.tansuo365.test1.mapper.othermapper.DWuliaoMapper;
import com.tansuo365.test1.service.excelservice.WuliaoExcelService;
import com.tansuo365.test1.service.kucunservice.WuliaoKucunSearService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WuliaoExcelServiceImpl implements WuliaoExcelService {

    private static final Logger LOGGER =
            LoggerFactory.getLogger(WuliaoExcelServiceImpl.class);

    @Resource
    private DWuliaoMapper dWuliaoMapper;
    @Autowired
    private WuliaoKucunSearService wuliaoKucunSearService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dWuliaoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return dWuliaoMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insert(DWuliao dWuliao) {
        return 0;
    }

    @Override
    public int insertSelective(DWuliao dWuliao) {
        return 0;
    }

    @Override
    public int insertBatch(List<DWuliao> list) {
        int i = dWuliaoMapper.insertSelectiveBatch(list);
//        int kucunSearCode = wuliaoKucunSearService.insertWuliaoToKucunWhenWuliaoBiao(list);
        if(i>0){
            return 1;
        }else{
            return -1;
        }
    }

    @Override
    public int insertBatch(Map<String, Object> maps) {
        return dWuliaoMapper.insertSelectiveBatchMap(maps);
    }

    @Override
    public List<InfoData> selectInfoDataSelective(DWuliao dWuliao) {
        return dWuliaoMapper.findSelectiveInfoData(dWuliao);
    }

    @Override
    public List<InfoData> selectInfoDataByPKArr(Integer[] ids) {
        return dWuliaoMapper.selectInfoDataByPKArr(ids);
    }

    @Override
    public List<DWuliao> selectAll() {
        return null;
    }

    @Override
    public InfoData selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(DWuliao dWuliao) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(DWuliao dWuliao) {
        return 0;
    }

    @Override
    public List<InfoData> selectInfoDataByTypeId(Integer w_id, Integer o_id) {
        return dWuliaoMapper.selectInfoDataByTypeId(w_id,o_id);
    }

    @Override
    public List<ShengouDetails> batchSelectInfoDataByWuliaobianma(List<ShengouDetails> shengouDetailsList) {



        /*将List<ShengouDetails>中的物料编码提取出来放入wuliaobianmaList中*/
        List<String> wuliaobianmaList = new ArrayList<String>();
        shengouDetailsList.forEach(shengouDetails->wuliaobianmaList.add(shengouDetails.getWuliaobianma()));
        wuliaobianmaList.forEach(wuliaobianma -> System.out.println(wuliaobianma));
        List<DWuliao> dWuliaoList = dWuliaoMapper.batchSelectInfoDataByWuliaobianma(wuliaobianmaList);
        LOGGER.info(">>>>>>>dWuliaoList:"+dWuliaoList);
        LOGGER.info(">>>>>>>修改前的shengouDetailsList:"+shengouDetailsList);
        //将excel批量上传的申购单物料详情中的物料编码与物料表中的物料编码一一对比，并将物料表中的大类，物料名称和单位写回申购单物料详情中，防止用户填错物料编码以外的选项
        for (int i = 0; i <shengouDetailsList.size(); i++) {
            int k = 0;
            for (int j = 0; j < dWuliaoList.size(); j++) {
                if (shengouDetailsList.get(i).getWuliaobianma().equals(dWuliaoList.get(j).getWuliaobianma())){
                    shengouDetailsList.get(i).setDalei(dWuliaoList.get(j).getDalei());
                    shengouDetailsList.get(i).setMingchengguige(dWuliaoList.get(j).getMingchengguige());
                    shengouDetailsList.get(i).setDanwei(dWuliaoList.get(j).getDanwei());
                    //物料编码对应上了k=1
                    k = 1;
                }
            }
            if (k==0){
                //如果申购物料列表详情中的物料编码在物料表中没查到，就删除该申购物料列表详情
                shengouDetailsList.remove(i);
                i = i-1;
            }
        }
        return shengouDetailsList;
    }
}
